from case.lnitial.lnitial_page import LnitialTest
from common.unit import AndroidUI
from object.lnitial.lnitial_page import LnitialObject
from object.lnitial.systemsetting import SystemObject
import time


# 系统设置/还少一个返回按钮
class SystemBoxesTest:
    def __init__(self):
        self.driver = AndroidUI.get_webdriver()
        self.ln = LnitialTest()
        # self.ln.input_true_password()       #进系统设置界面
        self.common = LnitialObject()
        self.setting = SystemObject()

    # 判断是否在系统设置界面，不是就打开
    def check_system(self):
        try:
            self.setting.bag()
            # print('已经在系统设置界面')
        except:
            self.ln.input_true_password()

    # 点击服务器选择
    def click_server(self):
        self.check_system()
        try:
            self.setting.server_button().click()
            self.setting.choice_server('浙江')
            print("点击服务器选择成功")
            AndroidUI().write_result("点击服务器选择测试", "成功")
        except:
            print("点击服务器选择失败")
            AndroidUI.capture()
            AndroidUI().write_result("点击服务器选择", "失败")

    # 服务器选择浙江,city直接输入服务器名称
    def choice_server(self, city):

        try:
            self.setting.choice_server(city).click()
            self.setting.ok_button().click()
            self.common.system_button()
            print('选择服务器%s选择成功' % city)
            AndroidUI().write_result('选择服务器%s选择成功' % city, "成功")
        except:
            print('选择服务器%s选择失败' % city)
            AndroidUI.capture()
            AndroidUI().write_result("'选择服务器%s'%city", "失败")

    # 点击机型选择
    def click_type(self):
        self.check_system()
        try:
            self.setting.type_button().click()
            try:
                self.setting.choice_type("出码分离")
                # self.driver.find_element_by_text("数据")
                print("点击机型选择成功")
                AndroidUI().write_result("点击机型选择测试", "成功")
            except:
                print("点击机型选择失败")
                AndroidUI.capture()
                AndroidUI().write_result("点击机型选择", "失败")
        except:
            print("点击机型选择失败")
            AndroidUI.capture()
            AndroidUI().write_result("点击机型选择", "失败")
            # self.driver.quit()

    # 选择机型
    def choice_type(self, type):
        try:
            self.setting.choice_type(type).click()
            # self.driver.find_element_by_text("数据")
            self.setting.ok_button().click()
            self.common.system_button()
            print('机型%s选择成功' % type)
            AndroidUI().write_result('机型%s选择' % type, "成功")
            # 返回首页
            # self.back_lnitial()
        except:
            print('机型%s选择失败' % type)
            AndroidUI.capture()
            AndroidUI().write_result('机型%s选择' % type, "失败")
            # self.driver.quit()

    # 选择信息采集模式，科室装袋
    def check_bag(self):
        self.check_system()
        try:
            self.setting.bag().click()
            print("科室装袋选择成功")
            AndroidUI().write_result("科室装袋选择测试", "成功")
        except:
            print("科室装袋选择失败")
            AndroidUI.capture()
            AndroidUI().write_result("科室装袋选择", "失败")

    # 选择信息采集模式，科室装箱
    def check_box(self):
        self.check_system()
        try:
            self.setting.box().click()
            print("科室装箱选择成功")
            AndroidUI().write_result("科室装箱选择测试", "成功")
        except:
            print("科室装箱选择失败")
            AndroidUI.capture()
            AndroidUI().write_result("科室装箱选择", "失败")

    # 选择信息采集模式，溯源校准仪
    def check_cart(self):
        self.check_system()
        try:
            self.setting.cart_checke().click()
            print("溯源校准仪选择成功")
            AndroidUI().write_result("溯源校准仪选择测试", "成功")
        except:
            print("溯源校准仪选择失败")
            AndroidUI.capture()
            AndroidUI().write_result("科溯源校准仪选择", "失败")

    # 打开固定箱码
    def open_box(self):
        self.check_system()
        self.setting.swipe(350, 652, 350, 100)
        try:
            # time.sleep(5)
            self.setting.open_box()
            print('已经开启了固定箱码')
            AndroidUI().write_result("已经开启了固定箱码", "成功")
        except:
            self.setting.box_id().click()
            print('打开固定箱码成功')
            AndroidUI().write_result("打开固定箱码", "成功")
        try:
            # time.sleep(5)
            self.setting.open_box()
        except:
            print("打开固定箱码失败")
            AndroidUI.capture()
            AndroidUI().write_result("打开固定箱码", "失败")

    # 关闭固定箱码
    def close_box(self):
        self.check_system()
        self.setting.swipe(350, 652, 350, 100)
        try:
            # time.sleep(5)
            self.setting.close_box()
            print('已经关闭了固定箱码')
            AndroidUI().write_result("已经关闭了固定箱码", "成功")
        except:
            self.setting.box_id().click()
            self.setting.close_box()
            print('关闭固定箱码成功')
            AndroidUI().write_result("关闭固定箱码", "成功")
        # try:
        #     # time.sleep(5)
        #     self.setting.close_box()
        #     print()
        # except:
        #     print("关闭固定箱码失败")
        #     AndroidUI.capture()
        #     AndroidUI().write_result("关闭固定箱码", "失败")

    #关闭固定箱码，设置浙江，出码分离，科室装箱
    def start_unfixed_box_test(self):
        self.click_server()
        self.choice_server("浙江")
        self.click_type()
        self.choice_type("出码分离")
        # self.setting.swipe(350,652,350,100)
        self.check_box()
        self.close_box()
        self.driver.keyevent(4)



    #打开固定箱码，设置浙江，出码分离，科室装箱
    def start_fixed_box_test(self):
        self.click_server()
        self.choice_server("浙江")
        self.click_type()
        self.choice_type("出码分离")
        # self.setting.swipe(350,652,350,100)
        self.check_box()
        self.open_box()
        self.driver.keyevent(4)


if __name__ == '__main__':
    s = SystemBoxesTest()
    # sobj=SystemObject()
    # s.click_server()
    # s.choice_server("浙江")
    # s.click_type()
    # s.choice_type("出码分离")
    # sobj.swipe(350,652,350,100)
    # s.check_box()
    # s.check_bag()
    # s.check_cart()
    # s.start_unfixed_box_test()
    s.start_fixed_box_test()
    # s.close_box()

    # s.start_unfixed_box_test()
